package com.example.billbillbangumi;

import android.content.ContentValues;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class RegisterActivity extends AppCompatActivity {
    private EditText id,password;
    private MyDatabaseHelper dbhelper;
    int i=1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        id=(EditText)findViewById(R.id.id);
        password=(EditText)findViewById(R.id.password);
        dbhelper = new MyDatabaseHelper(this, "Bangumi.db", null, 12);
        Button button1=(Button)findViewById(R.id.button1);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(id.getText().toString().equals("")){
                    Toast.makeText(RegisterActivity.this,"请输入你的账号",Toast.LENGTH_SHORT).show();
                }
                else if(password.getText().toString().equals("")){
                    Toast.makeText(RegisterActivity.this,"请输入密码",Toast.LENGTH_SHORT).show();
                }
                SQLiteDatabase db = dbhelper.getWritableDatabase();
                Cursor cursor=db.rawQuery("select* from id where id=?",new String[]{id.getText().toString()});
                if (cursor.moveToFirst()) {
                            Toast.makeText(RegisterActivity.this,"已有此账号，请重新输入",Toast.LENGTH_SHORT).show();
                            cursor.close();
                }
                    else
                    {
                        ContentValues values=new ContentValues();
                        values.put("id",id.getText().toString());
                        values.put("password",password.getText().toString());
                        db.insert("id",null,values);
                        values.clear();
                        Toast.makeText(RegisterActivity.this,"注册成功",Toast.LENGTH_SHORT).show();
                    }
            }
        });


        Button button2=(Button)findViewById(R.id.button2);
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent=new Intent(RegisterActivity.this,MainActivity.class);
                startActivity(intent);
            }
        });

    }
}

